import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';

// 枚举，用于设置选中形态类型
enum SelectedShapeType {
  checkIcon, // 打勾样式
  hollowCircle, // 蓝色球中间挖空白色球样式
}

class CustomBottomPicker extends StatefulWidget {
  final String title;
  final double height;
  final List<Map<String, dynamic>> list;
  final Color selectedColor;
  final Color backgroundColor;
  final dynamic initialValue; // 单选时为单个值，多选时为List
  final Function(dynamic) onSelect; // 回调参数：单选时为单个值，多选时为List
  final SelectedShapeType selectedShapeType;
  final bool isMultiSelect; // 新增：是否为多选模式

  const CustomBottomPicker({
    Key? key,
    required this.title,
    this.height = 300,
    required this.list,
    this.selectedColor = Colors.blue,
    this.backgroundColor = Colors.white,
    required this.initialValue,
    required this.onSelect,
    this.selectedShapeType = SelectedShapeType.checkIcon,
    this.isMultiSelect = false, // 默认单选
  }) : super(key: key);

  @override
  State<CustomBottomPicker> createState() => _CustomBottomPickerState();
}

class _CustomBottomPickerState extends State<CustomBottomPicker> {
  int _selectedIndex = -1; // 单选索引
  List<int> _selectedIndices = []; // 多选索引列表

  @override
  void initState() {
    super.initState();
    // 初始化选中状态
    if (widget.isMultiSelect) {
      // 处理多选初始值
      if (widget.initialValue is List) {
        for (var value in widget.initialValue) {
          final index = widget.list.indexWhere((item) => item['value'] == value);
          if (index != -1) {
            _selectedIndices.add(index);
          }
        }
      }
    } else {
      // 处理单选初始值
      _selectedIndex = widget.list.indexWhere(
            (item) => item['value'] == widget.initialValue,
      );
    }
  }

  // 构建选中状态组件
  StatelessWidget? _buildTrailingWidget(bool isSelected) {
    if (widget.selectedShapeType == SelectedShapeType.checkIcon) {
      return isSelected
          ? Icon(
        Icons.check,
        color: widget.selectedColor,
      )
          : const Icon(
        Icons.check_box_outline_blank,
        color: Colors.grey,
      );
    } else if (widget.selectedShapeType == SelectedShapeType.hollowCircle) {
      return Container(
        width: 20,
        height: 20,
        decoration: BoxDecoration(
          shape: BoxShape.circle,
          border: Border.all(
            color: isSelected ? widget.selectedColor : Colors.grey,
            width: 2,
          ),
        ),
        child: isSelected
            ? Container(
          margin: const EdgeInsets.all(3),
          decoration: BoxDecoration(
            shape: BoxShape.circle,
            color: widget.selectedColor,
          ),
        )
            : null,
      );
    }
    return null;
  }

  // 获取选中的值（单选或多选）
  dynamic _getSelectedValues() {
    if (widget.isMultiSelect) {
      return _selectedIndices
          .map((index) => widget.list[index]['value'])
          .toList();
    } else {
      return _selectedIndex != -1 ? widget.list[_selectedIndex]['value'] : null;
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: widget.height,
      color: widget.backgroundColor,
      child: Column(
        children: [
          AppBar(
            title: Text(widget.title),
            backgroundColor: Colors.white54,
            automaticallyImplyLeading: false,
            actions: [
              TextButton(
                onPressed: () {
                  // 点击确定按钮时返回选中值
                  widget.onSelect(_getSelectedValues());
                  Navigator.of(context).pop();
                },
                child: Text(
                  '确定',
                  style: TextStyle(color: widget.selectedColor),
                ),
              ),
            ],
          ),
          Expanded(
            child: ListView.builder(
              itemCount: widget.list.length,
              itemBuilder: (_, index) {
                final item = widget.list[index];
                final isSelected = widget.isMultiSelect
                    ? _selectedIndices.contains(index)
                    : index == _selectedIndex;

                return ListTile(
                  title: Text(
                    item['label'],
                    style: TextStyle(
                      color: isSelected ? widget.selectedColor : Colors.black,
                    ),
                  ),
                  trailing: _buildTrailingWidget(isSelected),
                  onTap: () {
                    setState(() {
                      if (widget.isMultiSelect) {
                        // 处理多选逻辑
                        if (_selectedIndices.contains(index)) {
                          _selectedIndices.remove(index);
                        } else {
                          _selectedIndices.add(index);
                        }
                      } else {
                        // 处理单选逻辑
                        _selectedIndex = index;
                      }
                    });
                  },
                );
              },
            ),
          ),
        ],
      ),
    );
  }
}
